/*
 * SpeakerTask.c
 *
 *  Created on: 10-07-2012
 *      Author: Mati
 */

#include "ApplicationTasks.h"

void vSpeakerTask(void *pvParameters) {
	xSpeakerParamQueue = xQueueCreate( 2, 2*sizeof(uint16_t));							//kolejka przez która przesylane sa parametry glosnika: czas trwania sygnalu i czestotliwosc

	portTickType xLastWakeTime;
	portTickType freqency;
	uint16_t soundFreq, duration;

	//printf("Speaker Task\n\r");

/*
#ifdef	INCLUDE_uxTaskGetStackHighWaterMark == 1
	unsigned portBASE_TYPE uxHighWaterMark;
    // Inspect our own high water mark on entering the task.
	uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
	printf("Speaker Task Stack: %d\n\r", uxHighWaterMark);
#endif
*/

	while (1) {
		if( xQueueReceive( xSpeakerParamQueue, &( duration ), ( portTickType ) portMAX_DELAY ) ){		//odbierz z kolejki czas trwania, czekaj do nieskonczonosci
			freqency = duration/portTICK_RATE_MS;
			//printf("Got frequency\n\r");
		}
		if( xQueueReceive( xSpeakerParamQueue, &( soundFreq ), ( portTickType ) 10 ) ){					//odbierz czestotliwosc dzwieku
			//printf("Sound ON\n\r");
			if(1 == PVal.speaker)
			{
				SoundOn(soundFreq);																			//uruchom glosnik
				xLastWakeTime = xTaskGetTickCount();														//pobierz aktualny czas
				vTaskDelayUntil(&xLastWakeTime, freqency);													//czekaj do konca
				SoundOff();																					//wylacz dzwiek
			}
		}

		LED_Toggle(1<<5);

/*
		#ifdef	INCLUDE_uxTaskGetStackHighWaterMark == 1
		// Calling the function will have used some stack space, we would
		//therefore now expect uxTaskGetStackHighWaterMark() to return a
		//value lower than when it was called on entering the task.
			uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );
			printf("Speaker Task Stack: %d\n\r", uxHighWaterMark);
		#endif
*/

	}
}
